00001 #include "Camera.h"
00002 #include <fstream>
00003 using namespace std;
00004
00005
00006
00007 void circleAimlessly::operator ()(osg::Node * node, osg::NodeVisitor* nv)
00008 {
00009 osg::PositionAttitudeTransform* pat = dynamic_cast <osg::PositionAttitudeTransform*> (node);
00010 if (pat != NULL)
00011 {
00012 angle += osg::DegreesToRadians(1.0f);
00013 pat->setPosition( osg::Vec3(cosf(angle)*100.0f, sinf(angle)*100.0f, 20.0f));
00014 pat->setAttitude(osg::Quat(angle, osg::Vec3(0,0,1)) );
00015 }
00016 traverse(node, nv);
00017 }
00018
00019 void updateAccumulatedMatrix::operator()(osg::Node* node, osg::NodeVisitor* nv)
00020 {
00021 matrix = osg::computeWorldToLocal(nv->getNodePath() );
00022 traverse(node,nv);
00023
00024 }
00025
00026 osg::Matrix transformAccumulator::getMatrix()
00027 {
00028 return mpcb->matrix;
00029 }
00030 transformAccumulator::transformAccumulator()
00031 {
00032 parent = NULL;
00033 node = new osg::Node;
00034 mpcb = new updateAccumulatedMatrix();
00035 node->setUpdateCallback(mpcb);
00036 }
00037
00038 bool transformAccumulator::attachToGroup(osg::Group* g)
00039 {
00040 bool success = false;
00041 if (parent != NULL)
00042 {
00043 int n = parent->getNumChildren();
00044 for (int i = 0; i < n; i++)
00045 {
00046 if (node == parent->getChild(i) )
00047 {
00048 parent->removeChild(i,1);
00049 success = true;
00050 }
00051 }
00052 if (! success)
00053 {
00054 return success;
00055 }
00056 }
00057 g->addChild(node);
00058 return true;
00059 }
00060
00061
00062
00063 void followNodeMatrixManipulator::setByMatrix(const osg::Matrixd& mat)
00064 {
00065 theMatrix = mat;
00066 }
00067 void followNodeMatrixManipulator::setByInverseMatrix(const osg::Matrixd& mat)
00068 {
00069 theMatrix = mat.inverse(mat);
00070 }
00071 void followNodeMatrixManipulator::updateTheMatrix()
00072 {
00073 theMatrix = worldCoordinatesOfNode->getMatrix();
00074 }
00075
00076
00077
00078 osg::Matrixd followNodeMatrixManipulator::getInverseMatrix() const
00079 {
00080 osg::Matrixd m;
00081 m = theMatrix * osg::Matrixd::rotate(-M_PI/2.0, osg::Vec3(1,0,0) )
00082 * osg::Matrixd::rotate(M_PI/2.0, osg::Vec3(0,1,0) )
00083 * osg::Matrixd::translate(osg::Vec3(0,-100,-500)) ;
00084
00085
00086 return m;
00087 }
00088
00089 osg::Matrixd followNodeMatrixManipulator::getMatrix() const
00090 {
00091 return theMatrix;
00092 }
00093
00094 bool followNodeMatrixManipulator::handle(const osgGA::GUIEventAdapter&ea, osgGA::GUIActionAdapter&aa)
00095 {
00096 switch(ea.getEventType())
00097 {
00098 case (osgGA::GUIEventAdapter::FRAME):
00099 {
00100 updateTheMatrix();
00101 }
00102 case (osgGA::GUIEventAdapter::KEYDOWN):
00103 {
00104 if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
00105 {
00106 theMatrix = theMatrix * osg::Matrixd::rotate(-M_PI/2.0, osg::Vec3(1,0,0) )
00107 * osg::Matrixd::rotate(M_PI/2.0, osg::Vec3(0,1,0) )
00108 * osg::Matrixd::translate(osg::Vec3(0,-200,-500)) ;
00109
00110
00111 }
00112
00113 }
00114 }
00115 return false;
00116 }